package cc.javaee.bbs.task;

import cc.javaee.bbs.annotations.Task;
import cc.javaee.bbs.model.activity.KbrActivity;
import cc.javaee.bbs.service.activity.ActivityService;
import cc.javaee.bbs.tool.DateUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author xdw
 */
@Component
public class ActivityEndJob extends AbstractJobTemplate {

    @Resource
    private ActivityService activityService;

    //每10分钟执行
    @Override
    @Task("活动结束跑批任务")
    @Scheduled(cron = "${schedule.demo}")
    protected void execute() {

        KbrActivity kbrActivity = new KbrActivity();
        kbrActivity.setStatus(1);

        List<KbrActivity> kbrActivities = activityService.find(kbrActivity);
        if (CollectionUtils.isNotEmpty(kbrActivities)) {
            kbrActivities.stream()
                    .filter(e ->
                            // 获取活动状态为正在进行 ， 并且结束时间小于当前时间的对象
                            DateUtil.getDateInit(e.getEndTime()).getTime() < DateUtil.getDateInit(System.currentTimeMillis()).getTime()
                    ).forEach(e -> {
                        // 处理活动状态为正在进行， 并且结束时间小于当前时间的对象
                        activityService.activityFinish(e);
                    }
            );

        }
    }

}
